﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using TidyBlog.Util;
using TidyBlog.Entity;
using TidyBlog.BLL;
using TidyBlog.Data;
using TidyBlog.MVC;

namespace TidyBlog.MVC.WWW.Controllers
{
    /// <summary>
    /// 评论控制器 /Comment/{action}
    /// </summary>
    public class CommentController : FrontController
    {
        /// <summary>
        /// 获取评论数据
        /// </summary>
        /// <param name="articleId">文章编号</param>
        /// <param name="page">页码</param>
        /// <returns>评论数据</returns>
        protected DataPaging<Comment> GetCommentList(int articleId, int page)
        {
            Dictionary<string, string> queryParams = new Dictionary<string, string>();
            queryParams.Add("visibility", "1");
            queryParams.Add("articleid", articleId.ToString());

            var result = CommentBLL.List(queryParams, true, 10, page);
            if (result.Data != null)
            {
                foreach (var comment in result.Data)
                {
                    comment.Email = null;
                    comment.IP = null;
                    comment.PostTime = comment.PostTime.AddHours(ViewBag.Setup.TimeZone);
                }
            }

            return result;
        }

        /// <summary>
        /// 返回JSON格式的评论数据
        /// </summary>
        /// <param name="id">文章编号</param>
        /// <param name="page">页码</param>
        /// <returns></returns>
        public JsonResult List(int? articleId, int? page)
        {
            return Json(GetCommentList(articleId.GetValueOrDefault(), page.GetValueOrDefault()),
                JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 发表评论
        /// </summary>
        /// <param name="comment">评论</param>
        /// <param name="captcha">验证码</param>
        /// <returns>{ CommentData : [object], Message : [string] } 没有CommentData属性时为出错</returns>
        public JsonResult Post(Comment comment, string captcha)
        {
            var user = ViewBag.User as User;

            if (user == null && !CaptchaManager.Check("comment", captcha))
            {
                return Json(new
                {
                    Message = "验证码错误"
                });
            }

            if (user != null)
            {
                // 导入用户数据
                comment.AuthorId = user.UserId;
                comment.AuthorName = user.Nickname;
                comment.Email = user.Email;
                comment.HomePage = user.HomePage;
                comment.Visibility = 1;
            }
            else
            {
                comment.Visibility = 0;
            }
            comment.IP = Request.GetClientIP();

            try
            {
                CommentBLL.Create(comment);
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Message = ex.Message
                });
            }

            return Json(new
            {
                CommentData = comment.Visibility == 0 ? null : GetCommentList(comment.ArticleId, -1),
                Message = comment.Visibility == 0 ? "您发表的评论须经过审核才会显示，请耐心等待" : "发表成功"
            });
        }
    }
}
